package com.bs.service;

import com.bs.domain.ServiceInfo;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;

import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;




@Service
public class SlowSqlBySvcService {
    private String partition;
    private String startTime;
    private String endTime;
    private String code;
    private String svc;
    @Autowired
    ElasticsearchTemplate elasticsearchTemplate;
    private SearchResponse getResponse() {
        String index = "";
        if (!code.equals("")){
            index = "logstash-"+code+"-svcsql-*";
        }
        Client transportClient = elasticsearchTemplate.getClient();
        BoolQueryBuilder builder = QueryBuilders.boolQuery();
        builder.filter(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("category", "SVCSQL"))
            .must(QueryBuilders.termQuery("priority","4"))
            .must(QueryBuilders.rangeQuery("@timestamp").from(startTime).to(endTime).timeZone("+08:00")));
            builder.filter(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("partition", partition))
            .must(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("svc",svc))));


        SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch(index).setTypes("log");

        return searchRequestBuilder
            .setSize(10)
            .setQuery(builder)
            .addSort("logtime", SortOrder.DESC)
            .execute().actionGet();
    }
    public List<ServiceInfo> slowSqlResource(String startTime, String endTime,String partition, String svc,String code) {
        this.partition = partition;
        this.startTime = startTime;
        this.endTime = endTime;
        this.code = code;
        this.svc = svc;
         List<ServiceInfo> serviceInfoList = new ArrayList<>();
        SearchResponse response = getResponse();
        if (response.getHits().totalHits() != 0) {
            SearchHit[] searchHits = response.getHits().getHits();
            System.out.println(response.getHits().getHits());
            for (SearchHit searchHits1 : searchHits
                ) {
                // String svcName = (String) searchHits1.getSource().get("svc");
                int execTime = (int) searchHits1.getSource().get("exec_time");
                String partitionKey = (String) searchHits1.getSource().get("partition");
                String logmsg = (String) searchHits1.getSource().get("logmsg");
                String logTime = (String) searchHits1.getSource().get("logtime");
                ServiceInfo slowServiceInfo = new ServiceInfo(svc, logTime, execTime, partitionKey, logmsg);
                serviceInfoList.add(slowServiceInfo);
            }
        }
        return serviceInfoList;
    }
}
